package cn.tedu.serviceEdu.controller;


import cn.tedu.commonUtils.R;
import cn.tedu.serviceBase.exception.EducationException;
import cn.tedu.serviceEdu.entity.EduTeacher;
import cn.tedu.serviceEdu.query.TeacherQuery;
import cn.tedu.serviceEdu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author tedu.
 * @since 2023-05-08
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    EduTeacherService teacherService;

    @ApiOperation(value = "讲师列表")
    @GetMapping("/list")
    public R findAll(){
        List<EduTeacher> teachers = teacherService.list(null);
        return R.ok().data("items",teachers);
    }

    @ApiOperation(value = "逻辑删除讲师列表")
    @GetMapping("/deleteById/{id}")
    public R deleteTeacherById(@PathVariable String id){
        boolean flag = teacherService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "分页查询讲师")
    @GetMapping("/selectPage/{current}/{size}")
    public R findByPage(@PathVariable Integer current, @PathVariable Integer size){
        Page<EduTeacher> page = new Page(current,size);
        teacherService.page(page,null);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加僵尸")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        teacherService.save(eduTeacher);
        return R.ok();
    }

    @ApiOperation(value = "分页&条件")
    @PostMapping("/selectTeacher/{current}/{size}")
    public R findByPageWithCondition(@PathVariable Integer current, @PathVariable Integer size,
                                     @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page(current,size);
        teacherService.pageQuery(page,teacherQuery);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data(map);
    }

    @ApiOperation(value = "id查询讲师")
    @GetMapping("/selectById/{id}")
    public R selectById(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("item",eduTeacher);
    }

    @ApiOperation(value = "id修改讲师")
    @PutMapping("/updateTeacher")
    public R updateById(@RequestBody EduTeacher eduTeacher){
        teacherService.updateById(eduTeacher);
        return R.ok();
    }


}

